package com.sxt;
import com.sxt.GameUtils;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import com.sxt.BgObj;
import javax.swing.JFrame;

    public class GameWin extends JFrame{
        //游戏的状态：0未开始	1游戏中	2暂停	3通关失败	4通关成功
        static int state = 0;//游戏默认状态

        Image offscreenImage = null;
        int width = 600;
        int height = 600;
        //背景图对象
        BgObj bgObj = new BgObj(GameUtils.bgImg,0,-2000,2);
        //我方飞机对象
        PlanObj planObj = new PlanObj(GameUtils.PlaneImg,290,550,20,30,0,this);
        //创建我方子弹的对象
        ShellObj shellObj = new ShellObj(GameUtils.shellImg,planObj.getX()+3,planObj.getY()-16,14,29,5,this);
        //创建启动方法
        private void launch() {
            // TODO Auto-generated method stub
            //设置窗口是否可见
            this.setVisible(true);
            //设置窗口大小
            this.setSize(width,height);
            //设置窗口位置
            this.setLocationRelativeTo(null);//默认为屏幕的中央
            //设置窗口标题
            this.setTitle("飞机大战");

            this.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    // TODO Auto-generated method stub
                    super.mouseClicked(e);
                    if(e.getButton()==1 && state == 0 ) {//判断是否是鼠标左键按下
                        state =1 ;
                        repaint();
                    }
                }
            });
            while(true) {
                repaint();
                try {
                    Thread.sleep(25);//25ms循环绘画一次
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            }

        }

        @Override
        public void paint(Graphics g) {
            // TODO Auto-generated method stub
            if(offscreenImage == null) {
                offscreenImage = this.createImage(width,height);
            }
            Graphics gImage = offscreenImage.getGraphics();
            //设置背景颜色
//    	gImage.setColor(Color.gray);
            gImage.fillRect(0, 0, width, height);
            gImage.setColor(Color.BLACK);
            gImage.setFont(new Font("仿宋", Font.BOLD, 40));
            if(state == 0) {
                gImage.drawImage(GameUtils.bgImg, 0,0,null);
                gImage.drawImage(GameUtils.bossImg, 220,120,50,50, null);
                gImage.drawImage(GameUtils.explodeImg, 270,350, null);

                gImage.drawString("点击开始游戏", 180, 300);
            }
            if (state == 1) {
                bgObj.paintself(gImage);
                planObj.paintself(gImage);
                shellObj.paintself(gImage);
            }
            gImage.drawImage(offscreenImage, 0, 0,null);
        }

        public static void main(String[] args) {
            GameWin ganWin = new GameWin();
            ganWin.launch();

        }
    }


